package com.office.common.utils.email;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 异步发送邮件
 *
 * @author: 柒月
 * @date: 2025/5/31 下午3:12
 * @version: 1.0
 */
@Component
public class EmailAsyncWrapper {


    @Autowired
    private EmailUtil emailUtil;

    private static final Logger log = LoggerFactory.getLogger(EmailAsyncWrapper.class);

    @Async("emailTaskExecutor")
    public void sendBatchEmailAsync(String from, String to, String subject, String text) {
        emailUtil.sendSimpleEmail(from, to, subject, text);
    }

    /**
     * 发送简单文本邮件
     *
     * @param from    发件人邮箱
     * @param toList  收件人邮箱集合
     * @param subject 邮件主题
     * @param text    邮件正文内容
     */
    @Async("emailTaskExecutor")
    public void sendBatchEmailAsync(String from, List<String> toList, String subject, String text) {
        for (String to : toList) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            emailUtil.sendSimpleEmail(from, to, subject, text);
        }
    }


    /**
     * 异步发送提醒电子邮件
     *
     * @param from    发件人邮箱
     * @param to      收件人邮箱
     * @param subject 邮件主题
     * @param text    邮件正文内容
     */
    @Async("emailTaskExecutor")
    public void sendReminderEmailAsync(String from, String to, String subject, String text) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        // 发送邮件
        emailUtil.sendSimpleEmail(from, to, subject, text);
    }

    /**
     * 异步发送 HTML 格式邮件
     */
    @Async("emailTaskExecutor")
    public void sendHtmlEmailAsync(String from, String to, String subject, String htmlContent) {
        try {
            emailUtil.sendHtmlEmail(from, to, subject, htmlContent);
        } catch (Exception e) {
            log.error("发送 HTML 邮件失败，收件人: {}", to, e);
        }
    }

    @Async("emailTaskExecutor")
    public void sendHtmlEmailAsync(String from, List<String> to, String subject, String htmlContent) {
        try {
            for (String t : to) {
                Thread.sleep(1000);
                emailUtil.sendHtmlEmail(from, t, subject, htmlContent);
            }
        } catch (Exception e) {
            log.error("发送 HTML 邮件失败，收件人: {}", to, e);
        }
    }


    /**
     * 异步发送带附件的邮件
     */
    @Async("emailTaskExecutor")
    public void sendEmailWithAttachmentAsync(String from, String to, String subject, String text, String attachmentPath) {
        try {
            emailUtil.sendEmailWithAttachment(from, to, subject, text, attachmentPath);
        } catch (Exception e) {
            log.error("发送带附件邮件失败，收件人: {}", to, e);
        }
    }

    @Async("emailTaskExecutor")
    public void sendEmailWithAttachmentAsync(String from, List<String> to, String subject, String text, String attachmentPath) {
        try {
            for (String t : to) {
                Thread.sleep(1000);
                emailUtil.sendEmailWithAttachment(from, t, subject, text, attachmentPath);
            }
        } catch (Exception e) {
            log.error("发送带附件邮件失败，收件人: {}", to, e);
        }
    }
}